using UnityEngine;
using System.Collections;

[System.Serializable]
public class OpenDoorTrigger : MonoBehaviour {
	
	public GameObject door;
	public string doorOpenAnim;
	public string doorCloseAnim;
	private bool opened = true;	
	
	private bool triggered = false;
	private bool useKeyPressed = false;
	
	// Use this for initialization
	void Start () {
		
	}
	
	void OnTriggerEnter(Collider other) {
		if (other.tag == "Player") {
			triggered = true;	
		}
	}
	
	void OnTriggerStay(Collider other)  {
		if (other.tag == "Player") {
			triggered = true;	
		}
	}
	
	void OnTriggerExit(Collider other) {
		if (other.tag == "Player") {
			triggered = false;	
		}
	}
	
	// Update is called once per frame
	void Update () {
		useKeyPressed = Input.GetKey("e");
		
		if (triggered && useKeyPressed && !door.animation.IsPlaying(doorCloseAnim) && !door.animation.IsPlaying(doorOpenAnim)) {
			if (opened) {
				door.animation.Play(doorCloseAnim);
				opened = false;
			} else {
				door.animation.Play(doorOpenAnim);
				opened = true;
			}
		}
	}
}
